﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Diagnostics;

namespace chad.performancecounters.library
{
    public struct PerformanceCounterItem
    {
        public String ObjectName;
        public String CounterName;
        public String InstanceName;
        public String CounterType;
        public String CounterHelp;
        //public long CollectedValue;

    }

    public class PerformanceCounterHelper
    {


        private static PerformanceCounterItem GetItemFromPerformanceCounter(PerformanceCounter counter)
        {
            PerformanceCounterItem item = new PerformanceCounterItem();

            item.ObjectName = counter.CategoryName;
            item.CounterName = counter.CounterName;
            item.InstanceName = counter.InstanceName;
            item.CounterHelp = counter.CounterHelp;
            item.CounterType = counter.CounterType.ToString();
            //item.CollectedValue = counter.RawValue;

            return item;
        }

        public static void CanConnectToComputer(String machineAddress)
        {
            PerformanceCounterCategory.GetCategories(machineAddress);
        }

        public static String[] GetAllPerformanceCounterCategoriesOnMachine(String machineAddress)
        {
            List<String> cats = new List<string>();
            foreach(var c in PerformanceCounterCategory.GetCategories(machineAddress))
            {
                cats.Add(c.CategoryName);
            }

            return cats.ToArray();
        }

        public static PerformanceCounterItem[] GetAllPerformanceCountersOnMachine(String machineAddress)
        {
            List<PerformanceCounterItem> collectedCounters = new List<PerformanceCounterItem>();

            try
            {
                //Parallel.ForEach(PerformanceCounterCategory.GetCategories(machineAddress).ToArray(), cat =>
                foreach(PerformanceCounterCategory cat in PerformanceCounterCategory.GetCategories(machineAddress))
                {
                    PerformanceCounterItem item = new PerformanceCounterItem();

                    item.ObjectName = cat.CategoryName;
                    item.CounterName = "";
                    item.InstanceName = "";
                    item.CounterType = "";
                    item.CounterHelp = cat.CategoryHelp;

                    collectedCounters.Add(item);

                    String[] instanceNames = cat.GetInstanceNames();

                    if(instanceNames.Count() == 0)
                    {
                        try
                        {
                            //Parallel.ForEach(cat.GetCounters().ToArray(), counter =>
                            foreach (PerformanceCounter counter in cat.GetCounters())
                            {
                                collectedCounters.Add(GetItemFromPerformanceCounter(counter));
                            }
                            //});
                        }
                        catch (Exception ex)
                        {


                        }
                    }
                    else
                    {
                        foreach(String instanceName in instanceNames)
                        {
                            try
                            {
                                //Parallel.ForEach(cat.GetCounters().ToArray(), counter =>
                                foreach (PerformanceCounter counter in cat.GetCounters(instanceName))
                                {
                                    collectedCounters.Add(GetItemFromPerformanceCounter(counter));
                                }
                                //});
                            } catch(Exception ex)
                            {
                                //TODO:
                            }

                        }

                    }
                }
                //});


            } catch (Exception ex)
            {
                //TODO:
            }

            return collectedCounters.ToArray();
        }


    }
}
